package de.bazinga.rotw.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.SharedNode;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.ObjToJme;

public class Model {
	
	public static Node loadModel(URL model,Vector3f pos,float scale,float angle) {
		SharedNode test = new SharedNode("Test",ModelCache.getInstance().getModel(model));

		test.setLocalScale(scale);
		test.setLocalTranslation(pos);
					
		Quaternion rotate = new Quaternion();
		rotate.fromAngleAxis(angle*FastMath.DEG_TO_RAD, new Vector3f(1,0,0));
		test.setLocalRotation(rotate);
		
		return test;
	}
	
	public static Node loadModel(URL model,Vector3f pos,float scale) {
		return Model.loadModel(model, pos, scale,270f);
	}
	
	
	public static Node getModel(String nodeName, String modelname) {
		if(ModelCache.getInstance().containsModel(modelname)) {
			SharedNode test = new SharedNode(nodeName,ModelCache.getInstance().getModel(modelname));

			return test;
		}
		return null;
	}
	
	
	
	
	
	
	
	public static void createJmeFrom3DS(URL oldFile,String newFileName) {
		String jmeGardenString = newFileName;
		FormatConverter converter = new MaxToJme();
		
		try{
			File jmeGardenFile = new File(jmeGardenString);
			
			FileOutputStream gardenOutStream = new FileOutputStream(jmeGardenFile);
			converter.convert(oldFile.openStream(), gardenOutStream);
		}catch(IOException e) {
			System.out.println("Fuck");
			System.exit(0);
		}
	}
	
	public static void createJmeFromObj(URL oldFile,String newFileName) {
		String jmeGardenString = newFileName;
		FormatConverter converter = new ObjToJme();
        converter.setProperty("mtllib", oldFile);

		try{
			File jmeGardenFile = new File(jmeGardenString);
			
			FileOutputStream gardenOutStream = new FileOutputStream(jmeGardenFile);
			converter.convert(oldFile.openStream(), gardenOutStream);
		}catch(IOException e) {
			System.out.println("Fuck");
			System.exit(0);
		}

	}
}
